package com.xiaotu.service.sw.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xiaotu.dao.sw.SwConcernMapper;
import com.xiaotu.dto.SwConcernDto;
import com.xiaotu.model.SwConcernModel;
import com.xiaotu.service.sw.SwConcernService;
import com.xiaotu.vo.SwConcernVo;
import com.xiaotu.vo.SwConcernsVo;
import com.xiaotu.vo.SwFansVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author zhangxiao
 * @create 2021-09-02 13:49
 */
@Service
@Transactional
public class SwConcernServiceImpl implements SwConcernService {

    @Autowired
    private SwConcernMapper swConcernMapper;

    @Override
    public Object saveConcern(SwConcernDto swConcernDto) {
        Example example = new Example(SwConcernModel.class);
        example.createCriteria()
                .andEqualTo("concernUserId", swConcernDto.getConcernUserId())
                .andEqualTo("beConcernStarId", swConcernDto.getBeConcernStarId());
        SwConcernModel model = new SwConcernModel();
        BeanUtil.copyProperties(swConcernDto, model);
        if (ObjectUtil.isNull(swConcernMapper.selectOneByExample(example))) {
            swConcernMapper.insert(model);
        } else {
            swConcernMapper.updateByExample(model, example);
        }
        return null;
    }

    @Override
    public SwConcernVo getConcernInfo(SwConcernDto swConcernDto) {
        Example example = new Example(SwConcernModel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("concernUserId", swConcernDto.getConcernUserId())
                .andEqualTo("beConcernStarId", swConcernDto.getBeConcernStarId());
        SwConcernModel swConcernModel = swConcernMapper.selectOneByExample(example);
        SwConcernVo swConcernVo = new SwConcernVo();
        if (ObjectUtil.isNull(swConcernModel)) {
            swConcernVo.setConcernStatus(0);
        } else {
            BeanUtil.copyProperties(swConcernModel, swConcernVo);
        }
        return swConcernVo;
    }

    @Override
    public List<SwFansVo> getMyFans(Integer starId) {
        return swConcernMapper.getMyFans(starId);
    }

    @Override
    public List<SwConcernsVo> getSwConcerns(Integer userId) {
        return swConcernMapper.getSwConcerns(userId);
    }

}
